<?php
class WCommon
{
	function truncatebig($source,$len='',$etc='')
	{
		if(!is_numeric($len) && $len < 1) return $source;
		$etc = @mb_convert_encoding($etc,'BIG-5','UTF-8');
		$source = @mb_convert_encoding($source,'UTF-8','BIG-5');
		return $source;
		if(mb_strlen($source) <= $len*2){
			return @mb_convert_encoding($source,'UTF-8','BIG-5');
		}else{
			return @mb_convert_encoding(mb_substr($source,0,$len),'UTF-8','BIG-5') . $etc;
		}
	}
	function truncate($source,$len='',$etc='')
	{
		if(!is_numeric($len) && $len < 1) return $source;
		$etc = @mb_convert_encoding($etc,SYSTEM_CODE,WEB_CODE);
		$source = @mb_convert_encoding($source,SYSTEM_CODE,WEB_CODE);
		if(mb_strlen($source) <= $len*2){
			return @mb_convert_encoding($source,WEB_CODE,SYSTEM_CODE);
		}else{
			return WCommon::left(@mb_convert_encoding($source,WEB_CODE,SYSTEM_CODE),($len*2-mb_strlen($etc))).@mb_convert_encoding($etc,WEB_CODE,SYSTEM_CODE);
		}
	}
	//过滤HTML代码再截取字符串
	function truncate_2($source,$len='',$etc='')
	{
		$source = strip_tags($source);
		$source = eregi_replace("&nbsp;","",$source);
		return WCommon::truncate($source,$len,$etc);
	}

	function left($source,$len='',$language='gb2312')
	{
		if(!is_string($source)) return false;
		if(!is_numeric($len) && $len < 1) return $source;
		$str = '';
		$source = @mb_convert_encoding($source,SYSTEM_CODE,WEB_CODE);
		if(mb_strlen($source) > $len){
			if($language == 'gb2312'){
				for($i=0;$i<$len;$i++){
					if($i==($len-1)){
						$str .= (ord($source[$i]) > 0xa0) ? '' : $source[$i];
					}else{
						$str .= (ord($source[$i]) > 0xa0) ? $source[$i].$source[++$i] : $source[$i];
					}

				}
			}else{
				$str = mb_substr($source,$len);
			}
		}else{
			$str = $source;
		}
		$str = @mb_convert_encoding($str,WEB_CODE,SYSTEM_CODE);
		return $str;
	}
	function right($source,$len='',$language='gb2312')
	{
		if(!is_string($source)) return false;
		if(!is_numeric($len) && $len < 1) return $source;
		$str = '';
		$source = @mb_convert_encoding($source,SYSTEM_CODE,WEB_CODE);
		if(mb_strlen($source) > $len){
			if($language == 'gb2312'){
				$length = mb_strlen($source)-1;
				$condition = $length-$len+1;
				$old=0;
				for($i=$length;$i>=$condition;$i--){
					ord($source[$i]) > 0xa0 && $old++;
				}
				($old%2==1) && $condition = $condition+1;
				for($i=$length;$i>=$condition;$i--){
					if($i==$condition){
						$str = ((ord($source[$i]) > 0xa0)  ? '' : $source[$i]).$str;
					}else{
						if(ord($source[$i]) > 0xa0){
							$str = ($source[($i-1)].$source[$i]).$str;
							$i--;
						}else{
							$str = $source[$i].$str;
						}
					}
				}
			}else{
				$str = mb_substr($source,-$len);
			}
		}else{
			$str = $source;
		}
		$str = @mb_convert_encoding($str,WEB_CODE,SYSTEM_CODE);
		return $str;
	}
	function parse_QueryString()
	{
		$QueryString			= $_SERVER["QUERY_STRING"];
		$arg_list				= func_get_args();
		$len					= count($arg_list);
		if($len > 0)
		{
			for($i=0;$i<$len;$i++)
			{
				$arg_list[$i]= "(^|&){$arg_list[$i]}=.[^&]*(&?)";
				$QueryString	= ereg_replace($arg_list[$i],"\\2",$QueryString);
			}
		}
		if($QueryString == "")
			$QueryString = "noparam=";
		return $QueryString;
	}

	function WHREF($const,$list = true,$NameValueCollection=array())
	{
		$href = "#";
		if(eregi("\\.(php|php3)$",$const) ||  eregi("/$",$const))
		{
			$href = $const;
		}
		else
		{
			if(isset($GLOBALS["SITE_CATALOG"][$const]["url"]))
			{
				if($list === true)
					$href = $GLOBALS["SITE_CATALOG"][$const]["url"];
				elseif($list === false)
					$href = eregi_replace("\\.(php|php5)$","_view.\\1",$GLOBALS["SITE_CATALOG"][$const]["url"]);
				else
					$href = eregi_replace("\\.(php|php5)$","$list.\\1",$GLOBALS["SITE_CATALOG"][$const]["url"]);
			}


			if ($href == "#" || $href == "")
			{
				if(isset($GLOBALS["SITE_CATALOG"][$const]))
				{
					$v = constant($const);
					$deep = 0;
					reset($GLOBALS["SITE_CATALOG"]);
					while (list($k,$value) = each($GLOBALS["SITE_CATALOG"])) {

						if($deep > $value["deep"])
							break;
						if($v == $value["root"])
						{
							if($value["url"] == "")
							{
								if(defined($k))
								{
									eval("\$v = $k;");
									$deep = $value["deep"];
								}
								else
									break;
							}
							else
							{
								$href = $value["url"];
								break;
							}
						}
					}
				}

				//if($const == "W_MARKET_YINGXIAOGAINIAN")
				//{ob_clean();print($href); print($const);print(constant($const));exit;}
				/*
				ob_clean();
				print_r($GLOBALS["SITE_CATALOG"]);
				print($href);
				exit;
				*/
				if($href == "#")
					return $href;
			}
		}

		$QueryString			= $_SERVER["QUERY_STRING"];
		if(is_array($NameValueCollection))
		{
			while (list($key,$value) = each($NameValueCollection))
			{
				$pattern = "(^|&){$key}=.[^&]*";
				$value = urlencode($value);
				if(ereg($pattern,$QueryString))
				{
					$QueryString = ereg_replace($pattern,"\\1$key=$value",$QueryString);
				}
				else if($QueryString == "")
				{
					$QueryString = "$key=$value";
				}
				else
				{
					$QueryString = "$QueryString&$key=$value";
				}

			}
		}
		if($QueryString != "")
			$QueryString = "?$QueryString";
		return $href . $QueryString;
	}

	function Query_Int($key,$default = 0)
	{
		if(isset($_GET[$key]) && ereg("^[0-9]+$",$_GET[$key]))
			return $_GET[$key];
		else
			return $default;
	}
	function Referer()
	{
		if(isset($_SERVER["HTTP_REFERER"]) && $_SERVER["HTTP_REFERER"] != "")
			return $_SERVER["HTTP_REFERER"];
		else
			return "";
	}
	function WBackUp()
	{
		$href = "";
		if(isset($_SERVER["HTTP_REFERER"]) && $_SERVER["HTTP_REFERER"] != "")
			$href = $_SERVER["HTTP_REFERER"];
		else 
			$href = "/";
		header("Location: $href");
		exit;
	}
	function unhtmlentities ($string) {
		$trans_tbl =get_html_translation_table (HTML_ENTITIES);
		$trans_tbl =array_flip ($trans_tbl );
		$string = str_replace("\n", "<br>", $string);
		$string = str_replace("&nbsp;", " ", $string);
		return strtr ($string ,$trans_tbl );
	}

	function dhtmlspecialchars($arr) {
	if(is_array($arr)){
		foreach ($arr as $key => $val){
			if (is_array($val)){
				dhtmlspecialchars($arr[$key]);
			}else{
				$arr[$key] = htmlspecialchars($val, ENT_QUOTES);
			}

		}
	} else{
		$arr =  htmlspecialchars($arr, ENT_QUOTES);
	}
	return $arr;
}

	function download($dir,$name=""){
		$dir = @mb_convert_encoding($dir,SYSTEM_CODE,WEB_CODE);
		$name = @mb_convert_encoding($name,SYSTEM_CODE,WEB_CODE);
		if(!file_exists($dir)){
  			echo "<script>alert('该文件不存在或已经被删除，请返回！');history.back();</script>";
  		 	exit;
	  	}
		if(!empty($name)){
			$filename = $name.strrchr($dir,".");
		}else{
			$filename = substr(strrchr(str_replace("\\","/",$dir),"/"),1);
		}
    	$filesize = filesize($dir);
    	header("Content-type: application/octet-stream");
    	header("Accept-Ranges: bytes");
   	 	header("Accept-Length: $filesize");
    	header("Content-Disposition: attachment; filename = $filename");
    	//@readfile($dir);
		if(($fp = @fopen($dir,'rb'))){
      		$content = fread($fp,$filesize);
	  		echo $content;
	  	fclose($fp);
		}
	}

	function changename($name,$i=0){
		return !empty($name) ?  preg_replace("/\.[a-z]{3,4}$/i","_$i\\0",$name) : false;
	}
	
	function WIP()
	{
		$ip = "Unknown";
		if ($HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"]) 
			$ip = $HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"]; 
		elseif ($HTTP_SERVER_VARS["HTTP_CLIENT_IP"]) 
			$ip = $HTTP_SERVER_VARS["HTTP_CLIENT_IP"]; 
		elseif ($HTTP_SERVER_VARS["REMOTE_ADDR"]) 
			$ip = $HTTP_SERVER_VARS["REMOTE_ADDR"]; 
		elseif (getenv("HTTP_X_FORWARDED_FOR")) 
			$ip = getenv("HTTP_X_FORWARDED_FOR"); 
		elseif (getenv("HTTP_CLIENT_IP")) 
			$ip = getenv("HTTP_CLIENT_IP"); 
		elseif (getenv("REMOTE_ADDR"))
			$ip = getenv("REMOTE_ADDR"); 
		return $ip;
	}
}
?>